package com.example.security.assembly;

import com.example.base.dto.TokenUserDTO;
import com.example.base.utils.JWTUtil;
import com.example.base.utils.RedisUtil;
import com.example.security.filter.JWTAuthenticationFilter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SysLogoutHandler implements LogoutHandler {

    @Override
    public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        //获取token 删除token
        String token = request.getHeader(HttpHeaders.AUTHORIZATION);
        if (StringUtils.isBlank(token)){
            return;
        }
        TokenUserDTO dto = JWTUtil.parseToken(token);
        //删除缓存
        RedisUtil.delete(JWTAuthenticationFilter.TOKEN_CACHE_PRE + dto.getUserId());
    }
}
